package com.ikas.ai.server.module.alarm.service;

import com.ikas.ai.consts.MachineAndWorkConditionConst;
import com.ikas.ai.enums.redis.RedisKeyEnum;
import com.ikas.ai.server.module.alarm.model.dto.AlarmStateDTO;
import com.ikas.ai.server.module.alarm.model.enums.AlarmType;
import com.ikas.ai.utils.ObjectUtil;
import com.ikas.ai.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AlarmCacheService {

    private final String ALARM_STATE_KEY = RedisKeyEnum.ALARM_STATE_KEY.key();

    @Autowired
    RedisUtil redisUtil;

    public void updateAlarm(AlarmType type, String machineNo, Integer num) {
        AlarmStateDTO cacheBean = redisUtil.getBean(ALARM_STATE_KEY, AlarmStateDTO.class);
        if (ObjectUtil.isNull(cacheBean)) {
            cacheBean = new AlarmStateDTO();
        }
        switch (machineNo) {
            case MachineAndWorkConditionConst.MACHINE_01_NO:
                cacheBean.getU01State().setAlarm(type, num);
                break;
            case MachineAndWorkConditionConst.MACHINE_02_NO:
                cacheBean.getU02State().setAlarm(type, num);
                break;
            case MachineAndWorkConditionConst.MACHINE_03_NO:
                cacheBean.getU03State().setAlarm(type, num);
                break;
            case MachineAndWorkConditionConst.MACHINE_04_NO:
                cacheBean.getU04State().setAlarm(type, num);
                break;
        }
        redisUtil.saveBean(ALARM_STATE_KEY, cacheBean, 86400);
    }

}
